package com.mdp.structural.composite.clone;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Composite implements IComponent {

    private List<IComponent> childNodeList = null;

    public Composite(boolean isTreeNode) {
        if (isTreeNode) {
            childNodeList = new ArrayList<IComponent>();
        }
    }

    @Override
    public void add(IComponent obj) {
        childNodeList.add(obj);
    }

    @Override
    public void operation(Object obj) {
        Object object = obj == null ? null : obj.toString();
        if (childNodeList != null) {
            System.out.println("++++chileNodeList++++" + object + "++++" + childNodeList + "+++++");
            Iterator<IComponent> it = childNodeList.iterator();
            while (it.hasNext()) {
                IComponent item = (IComponent) it.next();
                item.operation(item);
            }
        } else {
            System.out.println("++++LeafNode++++" + object + "++++");
        }

    }

    @Override
    public void remove(IComponent obj) {
        childNodeList.remove(obj);
    }

}
